home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Apple Guide / Engineering / Context Check Modules / MemFree CC / MemFree Source / memFree.c next >
Encoding:
C/C++ Source or Header  |  1993-11-08  |  1.9 KB  |  102 lines  |  [TEXT/KAHL]

  1. /*******************************************************
  2. *    
  3. *    memFree.c
  4. *
  5. *    external module for Apple Help
  6. *
  7. *    Author:         Josh Jacobs
  8. *    Copyright:        1993 Apple Computer, Inc
  9. *    Date:            5/3/93
  10. *
  11. */
  12.  
  13. #include "memFree.h"
  14.  
  15.  
  16.  
  17. /*******************************************************
  18. *
  19. *    Function prototypes
  20. *
  21. */
  22.  
  23. unsigned long GetFreeSystemMemory(unsigned long needed);
  24. OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize);
  25.  
  26.  
  27. /*******************************************************
  28. *
  29. *    main()
  30. *
  31. */
  32.  
  33. pascal OSErr
  34. main(memFreeDataPtr msg, Size inSize, void* outMessage, Size* outSize, Handle ignoreMe)
  35. {
  36.     Boolean            result = false;
  37.     OSErr            myErr = noErr;
  38.     unsigned long    curFreeMem,needed;
  39.     
  40.     needed = msg->amountNeeded * 1000;
  41.     
  42.     curFreeMem = GetFreeSystemMemory(needed);
  43.     
  44.     result = needed <= curFreeMem;
  45.     
  46.     myErr = SetContextResult(&result, sizeof(Boolean), outMessage, outSize);
  47.     return(myErr);
  48. }
  49.  
  50. /*******************************************************
  51. *
  52. *    GetFreeSystemMemory()
  53. *
  54. *    call tempFreeMem to see how much memory is available. 
  55. *    it calls compactMem only if the amount is not enough.
  56. *    this is to ensure that we are not thrashing the system
  57. *    heap by repeated compaction as Apple Help continues to evaluate
  58. *     our context.
  59. *
  60. */
  61.  
  62. unsigned long GetFreeSystemMemory(unsigned long needed)
  63. {
  64.     unsigned long    freeMem;
  65.     THz                oldZone    =    GetZone();
  66.  
  67.     SetZone(SystemZone());
  68.     freeMem = TempFreeMem();
  69.  
  70.     if (freeMem < needed)                  // Only compact if we need more memory
  71.         freeMem = CompactMem(needed);
  72.         
  73.     SetZone(oldZone);
  74.  
  75.     return(freeMem);
  76. }
  77.  
  78.  
  79. /*******************************************************
  80. *
  81. *    SetContextResult()
  82. *
  83. *    set the result up for Apple Help. 
  84. */
  85.  
  86. OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
  87. {
  88.     Ptr    p;
  89.     
  90.     if (p = NewPtr(theSize))
  91.     {
  92.         BlockMove(theData, p, theSize);
  93.         
  94.         *outSize        =    theSize;
  95.         *outMessage    =    p;
  96.         
  97.         return(noErr);
  98.     }
  99.     else
  100.         return(MemError());
  101. }
  102.